package com.example.userclient.custom

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View


class MapDetailView : View {

    constructor(context: Context?): super(context){

    }
    constructor(context: Context?, attrs: AttributeSet?): super(context, attrs){

    }
    private fun getMySize(defaultSize: Int, measureSpec: Int): Int {
        var mySize = defaultSize
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)
        Log.i("MapDetailView", "MeasureSpec mode is $mode, size is $size")
        when (mode) {
            MeasureSpec.UNSPECIFIED -> {
                //如果没有指定大小，就设置为默认大小
                mySize = defaultSize
            }
            MeasureSpec.AT_MOST -> {
                //如果测量模式是最大取值为size
                //我们将大小取最大值,你也可以取其他值
                mySize = size
            }
            MeasureSpec.EXACTLY -> {
                //如果是固定的大小，那就不要去改变它
                mySize = size
            }
        }
        return mySize
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        父View提供的参考大小
//        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
//        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
//        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
//        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        var width = getMySize(800, widthMeasureSpec)
        var height = getMySize(100, heightMeasureSpec)
        Log.i("MapDetailView", "custom width is $width, height is $height")
//        if (width < height) {
//            height = width
//        } else {
//            width = height
//        }

        setMeasuredDimension(width, height)

    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        //调用父View的onDraw函数，因为View这个类帮我们实现了一些
        // 基本的而绘制功能，比如绘制背景颜色、背景图片等
        val _debugInfoPaint = Paint(Paint.ANTI_ALIAS_FLAG) //设置无锯齿 也可以使用setAntiAlias(true)


        _debugInfoPaint.color = Color.GREEN //设置画笔颜色

        _debugInfoPaint.alpha = 200
        _debugInfoPaint.strokeWidth = 1.5f //设置线宽

        _debugInfoPaint.style = Paint.Style.STROKE //设置样式：FILL表示颜色填充整个；STROKE表示空心

        canvas.drawRoundRect(RectF(0F, 0F, width.toFloat(), height.toFloat()), 10F, 10F, _debugInfoPaint)
    }

}